package com.sxf.dao.impl;

import java.util.List;

import com.sxf.dao.UserDAO;
import com.sxf.domain.User;
import com.sxf.domain.UserMeta;
import com.sxf.domain.UserVO;
import com.sxf.mybatis.help.ExRowBounds;
import com.sxf.mybatis.help.QueryType;
import com.sxf.mybatis.page.Pagination;
import com.sxf.querybean.UserQuery;

public class UserDAOImpl extends SuperDAOImpl implements UserDAO {

	private static final String STATEMENT_QUERY = "mybatis.xml.User.query";

	@Override
	public List<UserVO> queryList(UserQuery query) {
		List<UserVO> userList = null;
		// userList = smct.queryForList("getUser", UserQuery);
		// Session session = sf.getCurrentSession();

		// Session hs = sf.openSession();

		// String hql = "from User";
		// Query query = hs.createQuery(hql);
		// userList = query.list();

		// userList = (List<User>) st.selectList(
		// "com.sxf.domain.User.getUserTest", user);

		userList = st.selectList(STATEMENT_QUERY, query,
				new ExRowBounds(query.getOffset(), query.getLimit(),
						QueryType.OBJECT));

		// new ExRowBounds(UserQuery.getOffset(), UserQuery.getLimit(),
		// QueryMode.OBJECT))

		// for (User us : userList) {
		// Set<UserMeta> umSet = us.getUserMetaSet();
		// for (UserMeta userMeta : umSet) {
		// System.out
		// .println(userMeta.getMetaKey() + "   "
		// + userMeta.getMetaValue() + "  "
		// + userMeta.getUserId());
		// }
		// }

		return userList;
	}

	@Override
	public UserVO queryOne(UserQuery query) {
		return st.selectOne(STATEMENT_QUERY, query);
	}

	@Override
	public Pagination<UserVO> queryPage(UserQuery query) {
		return queryPageTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public long queryNumber(UserQuery query) {
		return queryNumberTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public int insert(User info) {
		// Session session = sf.openSession();
		// session.save(User);
		if (info != null) {
			int i = st.insert("mybatis.xml.User.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int update(User info) {
		if (info != null) {
			return st.update("mybatis.xml.User.updateByPrimaryKeySelective",
					info);
		}
		return 0;
	}

	@Override
	public int delete(User info) {
		if (info != null) {
			return st.delete("mybatis.xml.User.deleteByPrimaryKey", info);
		}
		// sf.openSession().delete(user);
		return 0;
	}

	@Override
	public long insertUserMeta(UserMeta info) {
		if (info != null) {
			int i = st.insert("mybatis.xml.UserMeta.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int updateUserMeta(UserMeta info) {
		if (info != null) {
			return st.update(
					"mybatis.xml.UserMeta.updateByPrimaryKeySelective", info);
		}
		return 0;
	}

	@Override
	public int deleteUserMeta(UserMeta info) {
		if (info != null && (info.getId() != null || info.getUserId() != null)) {
			return st.delete("mybatis.xml.UserMeta.deleteByPrimaryKey", info);
		}
		return 0;
	}

}
